'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:UserIndexCtrl
 * @description
 * # UserIndexCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('UserIndexCtrl', function ($scope, UserInfo, $rootScope, openApi, $location, Util, UsertaskService, Modulemap, localStorageService, DataService) {
        $scope.picker = Util.getLocalInfo("picker") || {}
        $scope.tabList = [{
            title: "已通过"
        }, {
                title: "审核中"
            }, {
                title: "未批准"
            }];
        $scope.currentTab = Util.getTabIndex() || 1;
        $scope.onClickTab = function (index) {
            if ($scope.currentTab != index) {
                $scope.currentTab = index;
                getData();
                Util.clearLocalInfo("picker")
                $scope.picker = {}
            }
        }
        $scope.isActiveTab = function (index) {
            return index == $scope.currentTab;
        }
        // 去设置页面
        $scope.gosetting = function () {
            $location.path("user/setting")
        };
        // 去详情
        $scope.goTaskDetail = function (item) {
            var viewName;
            // 设置tabindex
            Util.setTabIndex("" + $scope.currentTab)
            viewName = getViewName(item);
            if (viewName) {
                // 用于自定义字段参数
                localStorageService.set("iconUrl", item.formKey);
                $location.path(viewName + "/" + item.id_);
            } else {
                Util.Toast("移动端暂不支持该流程的审批，请您在电脑上完成该操作");
                return;
            }
            Util.saveLocalInfo("picker", $scope.picker)
        };
        // 待审批，已通过 审核中 去查看页面只能查看
        // 未批准 去修改页面
        // 未处理 修改或者审批界面

        // 任务列表‘未处理’根据operationResult判断跳转方向，然后再判断是否有页面，如果没有就提示不支持该功能，并且不让跳转
        // operationResult：write是修改
        // operationResult：aduit审批
        var getViewName = function (item) {
            var viewName;
            var itemObj;
            itemObj = Modulemap.getViewNameAndIcon(item.formKey);
            // 先清除缓存数据
            Util.clearTVType()
            if ($scope.currentTab == 0 || $scope.currentTab == 1) {
                Util.setTVType(Util.CHECK)
                viewName = itemObj.checkViewName;
            } else if ($scope.currentTab == 2) {
                Util.setTVType(Util.UPDATE)
                viewName = itemObj.viewName;
            }
            return viewName;
        };
        // 过滤逻辑
        var hideDialog = function () {
            $rootScope.showMasker = false;
            $scope.showCerPicker = false;
        };
        $scope.cacelDialog = function () {
            hideDialog();
        };
        // 验证是否输入完整
        $scope.confirmDialog = function () {
            if (!$scope.picker.processName && !$scope.picker.keyWord) {
                return
            }
            getData($scope.picker);
            hideDialog();
        };
        var showDialog = function () {
            $scope.picker = {};
            DataService.getData(openApi.getAllprocess).then(function (data) {
                $scope.resiTypeList = data
                $rootScope.showMasker = true;
                $scope.showCerPicker = true;
            })
        };

        $scope.search = function () {
            showDialog()
        }
        var getData = function (options) {
            var param = {};
            param = angular.extend(param, options)
            param.flag = parseInt($scope.currentTab) + 1
            console.log(param);
            $scope.taskList = new UsertaskService(openApi.myProcessList, param);
            // 清除tabindex
            Util.clearTabIndex();
        };
        getData($scope.picker);
        UserInfo.getUseinfo().then(function (data) {
            $scope.UserInfo = data;
        });
    })
